<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Lawyer;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class LawyerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Lawyer(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('avatar')->image('', 120, 120);
            $grid->column('description')->limit(20);
            $grid->column('phone');
            $grid->column('law_firms');
            $grid->column('case_num');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Lawyer(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('avatar')->image();
            $show->field('description');
            $show->field('phone');
            $show->field('law_firms');
            $show->field('address');
            $show->field('case_num');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Lawyer(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->image('avatar')->autoUpload()->saveFullUrl();
            $form->textarea('description');
            $form->mobile('phone')->options(['mask' => '999 9999 9999']);
            $form->text('law_firms');
            $form->text('address');
            $form->number('case_num');

            $form->submitted(function (Form $form) {
                $form->phone = preg_replace('# #', '', $form->phone);
                return true;
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
